package cn.broadmax.boot.modules.sys.service.impl;

import cn.broadmax.boot.modules.sys.entity.SysResource;
import cn.broadmax.boot.modules.sys.entity.SysRoleResource;
import cn.broadmax.boot.modules.sys.entity.SysUserRole;
import cn.broadmax.boot.modules.sys.service.ISysResourceService;
import cn.broadmax.boot.modules.sys.service.ISysRoleResourceService;
import cn.broadmax.boot.modules.sys.service.ISysUserRoleService;
import cn.broadmax.boot.modules.sys.mapper.SysResourceMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jexx.util.CollectionUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源;菜单,按钮,页面 服务实现类
 * </p>
 *
 * @author jexx
 * @since 2020-07-21
 */
@Service
@RequiredArgsConstructor
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements ISysResourceService {

    private final ISysRoleResourceService sysRoleResourceService;
    private final ISysUserRoleService sysUserRoleService;

    @Override
    public List<SysResource> getResourcesByRoleId(String roleId) {
        LambdaQueryWrapper<SysRoleResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleResource::getRoleId, roleId);
        List<SysRoleResource> roleResources = sysRoleResourceService.list(wrapper);
        if(CollectionUtil.isEmpty(roleResources)){
            return Collections.emptyList();
        }
        List<String> resourceIds = roleResources.stream().map(SysRoleResource::getResourceId).collect(Collectors.toList());

        LambdaQueryWrapper<SysResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysResource::getId, resourceIds);
        return this.list(queryWrapper);
    }

    @Override
    public List<SysResource> getResourcesByUserId(String userId) {
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, userId);
        List<SysUserRole> sysUserRoles = sysUserRoleService.list(wrapper);
        if(CollectionUtil.isEmpty(sysUserRoles)){
            return Collections.emptyList();
        }
        List<String> roleIds = sysUserRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());

        LambdaQueryWrapper<SysRoleResource> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SysRoleResource::getRoleId, roleIds);
        List<SysRoleResource> roleResources = sysRoleResourceService.list(wrapper1);
        if(CollectionUtil.isEmpty(roleResources)){
            return Collections.emptyList();
        }
        List<String> resourceIds = roleResources.stream().map(SysRoleResource::getResourceId).collect(Collectors.toList());

        LambdaQueryWrapper<SysResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysResource::getId, resourceIds);
        queryWrapper.orderByAsc(SysResource::getSort);
        return this.list(queryWrapper);
    }
}
